--测试环境
--package.path = package.path .. ";?.lua"
--生产环境
package.path = "./plugin/HYDSXX/utils.lua" .. ";..\\?.lua"
package.path = "./plugin/HYDSXX/json.lua" .. ";..\\?.lua"
package.path = "./plugin/HYDSXX/rtu.lua" .. ";..\\?.lua"

require("utils")
require("json")
require("rtu")

--接收的报文
rxBuf = {}
--读数据与解析数据对应
Step = 0

--读变量
--在每次手动读变量/刷新时调用，或被自定义命令调用
function GenerateGetRealVariables(sAddr,step)
    Step = step
    if step == 0 then
        return rtu.ReadData(sAddr,03,0,2,true)
    elseif step == 1 then
        return rtu.ReadData(sAddr,04,2,3,false)
        --return rtu.ReadDate(sAddr,funCode,startAddr,regCnt,isCcontinue)
    --elseif step == 2 then 
        --......
    end
end

--自定义命令
function DeviceCustomCmd(sAddr, cmdName, cmdParam,step) 

    if (cmdName == "GetRealVariables") then --读寄存器
        return GenerateGetRealVariables(sAddr,step)
        --返回报文 
    elseif (cmdName == "SetVariables") then
        --[[
        local res = json.jsondecode(cmdParam)
        if res["XXX"] then
            rtu.WriteData(sAddr,funCode,startAddr,res["XXX"])
        end
        if res["YYY"] then
            rtu.WriteData(sAddr,funCode,startAddr,res["YYY"])
        end
        ]]
        -----
        -----
        -----
    end
end

--解析报文
function AnalysisRx(sAddr, rxBufCnt)
    local mAddr,mFunCode,mRegByte --地址、功能码、有效字节数
    if (rxBufCnt < 5) then
        rxBuf = {}
        VariableMap.Status = "1"
        return VariableMap  --字节数不足5位时返回 1 继续获取完整数据包
    end

    for index = 1, rxBufCnt, 1 do
        mAddr    = rxBuf[index]
        mFunCode = rxBuf[index + 1]
        mRegByte = rxBuf[index + 2]
        if (mAddr == tonumber(sAddr)) then
            if (mFunCode == 0x01 or 0x02 or 0x03 or 0x04) then
                if (rxBufCnt- index - 4 - mRegByte< 0) then
                    print("数据不完整")
                    VariableMap.Status = "1"
                    VariableMap.Variable = {}
                    rxBuf = {}
                    return VariableMap  --数据长度不完整时继续获取数据包
                end

                local rxCRC = rxBuf[index + 3 + mRegByte] +rxBuf[index + 4 + mRegByte]*256
                if rtu.CRCCheck(index,index+2+mRegByte,rxBuf,rxCRC) == false then
                    VariableMap.Status = "0"
                    VariableMap.Variable = {}
                    rxBuf = {}
                    return VariableMap
                end

                local dataIndex = index + 3 
                if (Step == 0) then
                    rtu.GetByteData({rxBuf[dataIndex],rxBuf[dataIndex+1]},1,"Distance")
                    local SNRfloat = rxBuf[dataIndex+2]*100 + rxBuf[dataIndex+3]
                    rtu.GetByteData({SNRfloat},1,"SNR")
                    --rtu.GetBitData(rxBuf[dataIndex],0,"Test")

                    VariableMap.Status = "0"
                    rxBuf = {}
                    return VariableMap
                elseif (Step == 1) then
                    rtu.GetByteData({rxBuf[dataIndex],rxBuf[dataIndex+1]},1,"Sensitivity")
                    VariableMap.Status = "0"
                    rxBuf = {}
                    return VariableMap
                    --..........
                end
            elseif (mFunCode == 0x81 or 0x82 or 0x83 or 0x84 or 0x85 or 0x86 or 0x90) then
                print("错误码"..string.format("%#x",mFunCode))
                VariableMap.Status = "0"
                VariableMap.Variable = {}
                rxBuf = {}
                return VariableMap
            elseif (mFunCode == 0x05 or 0x06 or 0x10) then
                if (rxBufCnt - 8 < 0) then
                    print("数据长度不够，没有数据域")
                    VariableMap.Status = "1"
                    VariableMap.Variable = {}
                    rxBuf = {}
                    return VariableMap
                end

                VariableMap.Status = "0"
                VariableMap.Variable = {}
                rxBuf = {}
                return VariableMap
            end
        end
    end
    rxBuf = {}
    return VariableMap
end

-- -- 测试环境下调试发送指令
-- if (TestEnv() == true) then --
--     --GenerateGetRealVariables(01, 1)
--     --DeviceCustomCmd(01, "SetVariables", 0, 0)
--     AnalysisRx("01", 11)
-- end